package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {
    private static void test1(Jedis jedis) {
        System.out.println("hset 和 hget");
        jedis.flushAll();

        jedis.hset("key","f1","111");
        HashMap<String ,String> fields=new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        jedis.hset("key",fields);
        String result = jedis.hget("key", "f1");
        System.out.println("result: " + result);
        result = jedis.hget("key", "f2");
        System.out.println("result: " + result);
        result = jedis.hget("key", "f3");
        System.out.println("result: " + result);
        result = jedis.hget("key", "f4");
        System.out.println("result: " + result);
    }
    private static void test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "111");
        jedis.hset("key", "f3", "111");
        boolean hexists = jedis.hexists("key", "f1");
        System.out.println("hexists: " + hexists);
        hexists = jedis.hexists("key","f2");
        System.out.println("hexists: " + hexists);
        hexists = jedis.hexists("key","f3");
        System.out.println("hexists: " + hexists);
        hexists = jedis.hexists("key","f4");
        System.out.println("hexists: " + hexists);
    }
    private static void test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "111");
        jedis.hset("key", "f3", "111");
        long del = jedis.hdel("key", "f1", "f2");
        System.out.println("del: " + del);
        boolean result = jedis.hexists("key", "f1");
        System.out.println("result: " + result);
        result = jedis.hexists("key", "f2");
        System.out.println("result: " + result);
        result = jedis.hexists("key", "f3");
        System.out.println("result: " + result);

    }
    private static void test4(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "222");
        jedis.hset("key", "f3", "333");
        Set<String> key = jedis.hkeys("key");
        System.out.println("key : "+ key);
        List<String> key1 = jedis.hvals("key");
        System.out.println("key1 : "+ key1);
    }
    private static void test5(Jedis jedis) {
        System.out.println("hmget 和 hmset");
        jedis.flushAll();
        Map<String ,String> map=new HashMap<>();
        map.put("f1", "111");
        map.put("f2", "222");
        map.put("f3", "333");
        jedis.hmset("key",map);
        List<String> hmget = jedis.hmget("key", "f1", "f3", "f2");
        System.out.println("hmget: " + hmget);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
